package it.giuseppetrisciuoglio.lyrics.processor;

import javax.annotation.Resource;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.DefaultProducerTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class BaseProcessor {

	@Resource(name="mainCamelContext")
	private CamelContext camelContext;
	
	@Value("${download.album.info.enabled}")
	private boolean downloadAlbumInfoEnabled;
	
	@Value("${download.lyrics.enabled}")
	private boolean downloadLyricsEnabled;
	
	@Value("${download.youtube.enabled}")
	private boolean downloadYoutubeEnabled;
	
	@Value("${artista.creator.enabled}")
	private boolean artistaCreatorEnabled;
	
	private final transient Logger log = LoggerFactory.getLogger(getClass());
	
	
	protected void sendMessageToAlbumInfoDownloadProcessor(Long id) {
		log.debug("sendMessageToAlbumInfoDownloadProcessor(): artistaId={}", id);
		log.debug("downloadAlbumInfoEnabled={}", downloadAlbumInfoEnabled);
		if (!downloadAlbumInfoEnabled){
			log.debug("sendMessageToAlbumInfoDownloadProcessor(): download album info processor is disabled.");
			return;
		}
		try {
			ProducerTemplate albumCoverTemplate = new DefaultProducerTemplate(camelContext, camelContext.getEndpoint("direct:startAlbumMetaData"));
			albumCoverTemplate.start();
			albumCoverTemplate.sendBodyAndHeader(id, "artistaId", id);
		} catch (Exception e) {
			log.error("sendMessageToArtistaCreatorProcessor(): exception={}", e.getMessage());
			throw new RuntimeException(e);
		}
		log.debug("sendMessageToAlbumInfoDownloadProcessor(): sended artistaId={}", id);
	}
	
	protected void sendMessageToArtistaCreatorProcessor(String nomeArtista) {
		log.debug("sendMessageToArtistaCreatorProcessor(): nomeArtista={}", nomeArtista);
		log.debug("artistaCreatorEnabled={}", artistaCreatorEnabled);
		if (!artistaCreatorEnabled){
			log.debug("sendMessageToArtistaCreatorProcessor(): artista creator processor is disabled.");
			return;
		}
		try {
			ProducerTemplate artistaCreatorProcessor = new DefaultProducerTemplate(camelContext, camelContext.getEndpoint("artistaCreatorJMS"));
			artistaCreatorProcessor.start();
			artistaCreatorProcessor.sendBody(nomeArtista);
		} catch (Exception e) {
			log.error("sendMessageToArtistaCreatorProcessor(): exception={}", e.getMessage());
			throw new RuntimeException(e);
		}
		log.debug("sendMessageToArtistaCreatorProcessor(): sended nomeArtista={}", nomeArtista);
	}
	
	protected void sendMessageToLyricsDownloadProcessor(Long id) {
		log.debug("sendMessageToLyricsDownloadProcessor(): artistaId={}", id);
		log.debug("downloadLyricsEnabled={}", downloadLyricsEnabled);
		if (!downloadLyricsEnabled){
			log.debug("sendMessageToLyricsDownloadProcessor(): download album info processor is disabled.");
			return;
		}
		try {
			ProducerTemplate lyricProducerTemplate = new DefaultProducerTemplate(camelContext, camelContext.getEndpoint("direct:startDownload"));
			lyricProducerTemplate.start();
			lyricProducerTemplate.sendBodyAndHeader(id, "artistaId", id);
		} catch (Exception e) {
			log.error("sendMessageToLyricsDownloadProcessor(): exception={}", e.getMessage());
			throw new RuntimeException(e);
		}
		log.debug("sendMessageToLyricsDownloadProcessor(): sended artistaId={}", id);
	}
	
	protected void sendMessageToYoutubeDownloadProcessor(Long id) {
		log.debug("sendMessageToYoutubeDownloadProcessor(): artistaId={}", id);
		log.debug("downloadYoutubeEnabled={}", downloadYoutubeEnabled);
		if (!downloadYoutubeEnabled){
			log.debug("sendMessageToYoutubeDownloadProcessor(): download album info processor is disabled.");
			return;
		}
		try {
			ProducerTemplate youtubeProducerTemplate = new DefaultProducerTemplate(camelContext, camelContext.getEndpoint("direct:startYoutube"));
			youtubeProducerTemplate.start();
			youtubeProducerTemplate.sendBody("direct:startYoutube", id);
		} catch (Exception e) {
			log.error("sendMessageToYoutubeDownloadProcessor(): exception={}", e.getMessage());
			throw new RuntimeException(e);
		}
		log.debug("sendMessageToYoutubeDownloadProcessor(): sended artistaId={}", id);
	}
}